package com.offer;

import com.bean.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: huangzhigao
 * @Date: 2023/3/4 18:29
 */
public class offer34 {
    private List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root, target, 0, new ArrayList<>());
        return res;
    }

    public void dfs(TreeNode root, int target, int sum, List<Integer> oneAnswer) {
        if (root == null) {
            return;
        }
        oneAnswer.add(root.val);

        if (root.left == null && root.right == null && sum == target - root.val) {
            res.add(new ArrayList<>(oneAnswer));
        }

        dfs(root.left, target, sum + root.val, oneAnswer);
        dfs(root.right, target, sum + root.val, oneAnswer);
        oneAnswer.remove(oneAnswer.size() - 1);
    }
}
